home *** CD-ROM | disk | FTP | other *** search
- Path: ub239.dialup.uwa.edu.au!not-for-mail
- From: prye@cyllene.uwa.edu.au (Peter Rye)
- Newsgroups: comp.lang.c++
- Subject: Re: precision methods
- Date: 29 Jan 1996 00:53:53 +0800
- Organization: The University of Western Australia
- Message-ID: <4eg9n1$24u@ub239.dialup.uwa.edu.au>
- References: <4ebq07$54r@sun.cis.smu.edu>
- NNTP-Posting-Host: ub239.dialup.uwa.edu.au
-
- dbowman@post.smu.edu (Damon Bowman) writes:
-
- >How is the precision member functions supposed to work? The book says
- >that it controls the number of places to the RIGHT of the decimal. In
- >other words,
-
- >float a = 45.67;
- >cout.precision(2);
- >cout << a;
-
- >Should output 45.67. It actually outputs 46 on my machine. Is the
- >book wrong (wouldn't be the first time I've found blatant errors in
- >programming books).
-
- This is what it does here also.
- The setting of precision does slightly different things depending on
- how the other ios flags are set.
- In my case and presumably yours, 'ios::fixed' is unset as the default.
- In this case precision will set the number of *significant digits* in
- the output. If ios::fixed is set, it will give you the number of
- digits to the right of the decimal place.
-
- eg:
-
- ub239:~$ cat test1.cpp
- #include<iostream.h>
- #include<iomanip.h>
-
- int main() {
-
- float x = 46.35923;
-
- cout.setf(ios::fixed);
- cout.precision(5);
- cout << x << endl;
-
- cout.unsetf(ios::fixed);
- cout << x << endl;
-
- return 0;
- }
- ub239:~$ g++ -Wall test1.cpp
- ub239:~$ a.out
- 46.35923
- 46.359
-
-
- >d:\msvc\test\fig11_17.cpp(21) : warning C4270: 'initializing' : do not
- >initialize a non-const 'class ::__SMANIP_int __near &' with a
- >non-lvalue 'class ::__SMANIP_int ' function return
-
- Can't help sorry. I get no warnings/errors with your code here.
- The code doesn't give the results as depicted in the book though (I've
- got Deitel & Deitel too), but does give the results you'd expect as
- explained above.
-
- Regards,
- Peter Rye
- --
- | Peter Rye |
- | Respiratory Research Fellow |
- | Princess Margaret Hospital for Children |
- | Perth, Western Australia |
-